package com.ruoyi.net.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.net.domain.*;
import com.ruoyi.net.mapper.BusiL2VpnMapper;
import com.ruoyi.net.mapper.BusiL2VpnPwMapper;
import com.ruoyi.net.service.IBusiL2VpnPwService;
import com.ruoyi.net.service.IBusiL2VpnTunnelStrategyService;
import com.ruoyi.net.utils.autoIncrement.GlobalIncrementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务-L2 VPN配置-pw列表Service业务层处理
 * 
 * @author net
 * @date 2024-12-14
 */
@Service
public class BusiL2VpnPwServiceImpl implements IBusiL2VpnPwService 
{
    @Autowired
    private BusiL2VpnPwMapper busiL2VpnPwMapper;

    @Autowired
    private BusiL2VpnMapper busiL2VpnMapper;

    @Autowired
    private IBusiL2VpnTunnelStrategyService tunnelStrategyService;

    @Autowired
    private GlobalIncrementService globalIncrementService;

    /**
     * 查询业务-L2 VPN配置-pw列表
     * 
     * @param id 业务-L2 VPN配置-pw列表主键
     * @return 业务-L2 VPN配置-pw列表
     */
    @Override
    public BusiL2VpnPw selectBusiL2VpnPwById(String id)
    {
        return busiL2VpnPwMapper.selectBusiL2VpnPwById(id);
    }

    /**
     * 查询业务-L2 VPN配置-pw列表列表
     * 
     * @param busiL2VpnPw 业务-L2 VPN配置-pw列表
     * @return 业务-L2 VPN配置-pw列表
     */
    @Override
    public List<BusiL2VpnPwListVo> selectBusiL2VpnPwList(BusiL2VpnPw busiL2VpnPw)
    {
        return busiL2VpnPwMapper.selectBusiL2VpnPwList(busiL2VpnPw);
    }

    /**
     * 新增业务-L2 VPN配置-pw列表
     * 
     * @param busiL2VpnPw 业务-L2 VPN配置-pw列表
     * @return 结果
     */
    @Override
    public int insertBusiL2VpnPw(BusiL2VpnPw busiL2VpnPw)
    {
        String uuid = IdUtils.simpleUUID();
        if (StringUtils.isEmpty(busiL2VpnPw.getId())) {
            busiL2VpnPw.setId(uuid);
        }
        String pwclassnameIncrementNumber = globalIncrementService.getNextPwClassNameIncrementNumber();
        // 自动生成 todo 2025-02-17 添加
        busiL2VpnPw.setPwClassName("pwclassname-" + pwclassnameIncrementNumber);
        // todo 2025-03-04 经和小二、美玲沟通，in / out label 用同一个值
        String label = globalIncrementService.getNextOutLabelIncrementNumber();
        busiL2VpnPw.setOutLabel(label);
        busiL2VpnPw.setInLabel(label);

        // 自动生成 todo 2025-02-18 添加
        // vcid 对应标签 pw-id
        busiL2VpnPw.setVcId(globalIncrementService.getNextVcIdIncrementNumber());
        busiL2VpnPw.setControlEnable("enable");
        busiL2VpnPw.setTunnelSwitch("disable");

        busiL2VpnPw.setCreateBy(SecurityUtils.getUsername());
        busiL2VpnPw.setCreateTime(DateUtils.getNowDate());
        busiL2VpnPw.setUpdateBy(SecurityUtils.getUsername());
        busiL2VpnPw.setUpdateTime(DateUtils.getNowDate());

        return busiL2VpnPwMapper.insertBusiL2VpnPw(busiL2VpnPw);
    }

    /**
     * 修改业务-L2 VPN配置-pw列表
     * 
     * @param busiL2VpnPw 业务-L2 VPN配置-pw列表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBusiL2VpnPw(BusiL2VpnPw busiL2VpnPw)
    {
        // 写入 隧道策略表 todo 2025-02-17, 如何判断该业务已经存在
        BusiL2VpnTunnelStrategy tunnelStrategy = new BusiL2VpnTunnelStrategy();
        tunnelStrategy.setL2VpnId(busiL2VpnPw.getL2VpnId());
        tunnelStrategy.setTunnelType(busiL2VpnPw.getTunnelType());
        tunnelStrategy.setTunelBusiId(busiL2VpnPw.getServiceLayerId());

        List<BusiL2VpnTunnelStrategy> list = tunnelStrategyService.selectBusiL2VpnTunnelStrategyList(tunnelStrategy);
        if (list == null || list.isEmpty()) {
            tunnelStrategyService.insertBusiL2VpnTunnelStrategy(tunnelStrategy);
        }

        busiL2VpnPw.setUpdateBy(SecurityUtils.getUsername());
        busiL2VpnPw.setUpdateTime(DateUtils.getNowDate());
        return busiL2VpnPwMapper.updateBusiL2VpnPw(busiL2VpnPw);
    }

    /**
     * 批量删除业务-L2 VPN配置-pw列表
     * 
     * @param ids 需要删除的业务-L2 VPN配置-pw列表主键
     * @return 结果
     */
    @Override
    public int deleteBusiL2VpnPwByIds(String[] ids)
    {
        return busiL2VpnPwMapper.deleteBusiL2VpnPwByIds(ids);
    }

    /**
     * 删除业务-L2 VPN配置-pw列表信息
     * 
     * @param id 业务-L2 VPN配置-pw列表主键
     * @return 结果
     */
    @Override
    public int deleteBusiL2VpnPwById(String id)
    {
        return busiL2VpnPwMapper.deleteBusiL2VpnPwById(id);
    }
}
